//
//  XcodeCleanVC.swift
//  ZJTools
//
//  Created by 万孟超 on 2023/11/11.
//

import Cocoa
import SnapKit

class XcodeCleanVC: NSViewController {
    @IBOutlet weak var rightDetailView: NSView!
    @IBOutlet weak var leftItemView: NSView!
    
    let homeVC = XcodeCleanHomeVC()
    let one = XcodeCleanResultVC(cleanType: .deriveData)
    let two = XcodeCleanResultVC(cleanType: .cache)
    let three = XcodeCleanResultVC(cleanType: .archive)
    let four = XcodeCleanResultVC(cleanType: .deviceSupport)
    let five = XcodeCleanResultVC(cleanType: .simulator)
    
    lazy var viewModel = XcodeCleanVM(itemChangeBlock: changeItem(_:))
    lazy var leftTabView: XcodeCleanItemV = {
        let tabVie = XcodeCleanItemV(itemArray: viewModel.itemArray)
        return tabVie
    }()
    override func viewDidLoad() {
        super.viewDidLoad()
        
        leftItemView.wantsLayer = true
        leftItemView.layer?.masksToBounds = true
        leftItemView.layer?.backgroundColor = NSColor.white.withAlphaComponent(0.25).cgColor
        
        rightDetailView.wantsLayer = true
        rightDetailView.layer?.backgroundColor = NSColor.white.withAlphaComponent(0.4).cgColor
        installItemView()
        installChild()
        registerNotification()
    }
    
    func installItemView() {
        leftItemView.addSubview(leftTabView)
        leftTabView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview().offset(15)
            make.size.equalTo(NSMakeSize(160, CGFloat(viewModel.itemArray.count * 80)))
        }
    }
    
    func registerNotification() {
        NotiManager.add(self, name: .XcodeCleanReceive) {[self] _, userInfo in
            if let index = userInfo?["index"] as? Int {
                if let isFreshFloder = userInfo?["isFreshFloder"] as? Bool, isFreshFloder {
                    if index == 1 {
                        one.refreshData()
                    }else if index == 2 {
                        two.refreshData()
                    }else if index == 3 {
                        three.refreshData()
                    }else if index == 4 {
                        four.refreshData()
                    }else if index == 5 {
                        five.refreshData()
                    }
                }else if let size = userInfo?["size"] as? Double {
                    leftTabView.freshItemSize(index, size: size)
                    let totleSize = XcodeCleanManager.shared.archiveModel.size + XcodeCleanManager.shared.deriveDataModel.size + XcodeCleanManager.shared.deviceSupportModel.size + XcodeCleanManager.shared.cacheModel.size + XcodeCleanManager.shared.simulatorModel.size
                    leftTabView.freshItemSize(0, size: totleSize)
                }
            }
        }
    }
    
    func installChild() {
        addChild(homeVC)
        addChild(one)
        addChild(two)
        addChild(three)
        addChild(four)
        addChild(five)
        rightDetailView.addSubview(homeVC.view)
    }
    
    func changeItem(_ itemIndex: Int) {
        transition(from: children[viewModel.currentIndex], to: children[itemIndex], options: .allowUserInteraction)
    }
    
    
    deinit {
        NotiManager.remove(self)
    }
}
